package modules.user.service.impl;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import modules.user.dao.UserDao;
import modules.user.data.AccountProfile;
import modules.user.data.BadgesCount;
import modules.user.data.UserVO;
import modules.user.entity.User;
import modules.user.service.UserService;

@Service
/*
 * readOnly=true表明所注解的方法或类只是读取数据。
 * readOnly=false表明所注解的方法或类是增加，删除，修改数据。
 */
@Transactional
/*
 * * 在类上面统一定义缓存的名字，方法上面就不用标注了*
 * 当标记在一个类上时则表示该类所有的方法都是支持缓存的*
 */
@CacheConfig(cacheNames="usersCaches")
public class UserServiceImpl implements UserService{

	@Autowired
	private UserDao userDao;
	@Autowired
	private NotifyService notifyService;
	@Autowired
	private RoleDao roleDao;
	
	@Override
	@Transactional
	public AccountProfile login(String username, String password) {
		User po = userDao.findByUsername(username);
		
		AccountProfile u = null;
		
		//判断po是否为空，空的话抛出非法数据异常，提示（账户不存在）
		Assert.notNull(po,"账户不存在");
		//Assert.state(expression, message); 判断expression（Boolean）是否为false，是的话抛出非法数据异常，提示message
		Assert.state(StringUtils.equals(po.getPassword(), password), "密码错误");
		po.setLastLogin(Calendar.getInstance().getTime());
		userDao.save(po);
		u = BeanMapUtils.copyPassport(po);
		BadgesCount badgesCount = new BadgesCount();
		badgesCount.setNotifies(notifyService.unread4Me(u.getId()));
		u.setBadgesCount(badgesCount);
		return u;
	}

	@Override
	public AccountProfile getProfileByName(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserVO register(UserVO user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AccountProfile update(UserVO user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AccountProfile updateEmail(long id, String email) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserVO get(long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserVO getByUsername(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AccountProfile updateAvatar(long id, String path) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void updatePassword(long id, String newPassword) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updatePassword(long id, String oldPassword, String newPassword) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateStatus(long id, int status) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public AccountProfile updateActiveEmail(long id, int activeEmail) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<UserVO> paging(Pageable pageable) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<Long, UserVO> findMapByIds(Set<Long> ids) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserVO> findHotUserByfans() {
		// TODO Auto-generated method stub
		return null;
	}

}
